using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using Crosland.Framework.DataHelper;
using Crosland.Framework.Base;
using Crosland.Offline.Core;
using Crosland.Offline.Data.Helper;

namespace Crosland.Offline.Data.Dao
{
    public class UsuarioDao : DaoBase
    {
        public UsuarioDao(string strConn) : base(strConn)
        {
        }

        UsuarioHelper helper = new UsuarioHelper();

        public int Save(Usuario entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpSave);

            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, entity.ID);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, entity.DesNombre);
            dbProvider.AddInParameter(command, helper.DesApellido, DbType.String, entity.DesApellido);
            dbProvider.AddInParameter(command, helper.DesPassword, DbType.String, entity.DesPassword);
            dbProvider.AddInParameter(command, helper.DesEmail, DbType.String, entity.DesEmail);
            dbProvider.AddInParameter(command, helper.IndEstado, DbType.String, entity.IndEstado);
            dbProvider.AddInParameter(command, helper.Vkorg, DbType.String, entity.Vkorg);
            dbProvider.AddInParameter(command, helper.Spart, DbType.String, entity.Spart);
            dbProvider.AddInParameter(command, helper.Vkgrp, DbType.String, entity.Vkgrp);
            dbProvider.AddInParameter(command, helper.Vkbur, DbType.String, entity.Vkbur);
            dbProvider.AddInParameter(command, helper.Werks, DbType.String, entity.Werks);
            dbProvider.AddInParameter(command, helper.Lgort, DbType.String, entity.Lgort);
            dbProvider.AddInParameter(command, helper.UsrCreacion, DbType.String, entity.UsrCreacion);
            dbProvider.AddInParameter(command, helper.FecCreacion, DbType.DateTime, entity.FecCreacion);

            return dbProvider.ExecuteNonQuery(command);
        }

        public void Update(Usuario entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpUpdate);

            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, entity.ID);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, entity.DesNombre);
            dbProvider.AddInParameter(command, helper.DesApellido, DbType.String, entity.DesApellido);
            dbProvider.AddInParameter(command, helper.DesPassword, DbType.String, entity.DesPassword);
            dbProvider.AddInParameter(command, helper.DesEmail, DbType.String, entity.DesEmail);
            dbProvider.AddInParameter(command, helper.IndEstado, DbType.String, entity.IndEstado);
            dbProvider.AddInParameter(command, helper.Vkorg, DbType.String, entity.Vkorg);
            dbProvider.AddInParameter(command, helper.Spart, DbType.String, entity.Spart);
            dbProvider.AddInParameter(command, helper.Vkgrp, DbType.String, entity.Vkgrp);
            dbProvider.AddInParameter(command, helper.Vkbur, DbType.String, entity.Vkbur);
            dbProvider.AddInParameter(command, helper.Werks, DbType.String, entity.Werks);
            dbProvider.AddInParameter(command, helper.Lgort, DbType.String, entity.Lgort);

            dbProvider.ExecuteNonQuery(command);
        }

        public void Delete(System.String id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpDelete);

            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, id);

            dbProvider.ExecuteNonQuery(command);
        }

        public Usuario GetById(System.String id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetById);

            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, id);
            Usuario entity = null;

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    entity = helper.Create(dr);
                }
            }

            return entity;
        }

        public List<Usuario> List()
        {
            List<Usuario> entitys = new List<Usuario>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpList);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<Usuario> GetByCriteria()
        {
            List<Usuario> entitys = new List<Usuario>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<Usuario> GetByCriteria(string nombre)
        {
            List<Usuario> entitys = new List<Usuario>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, nombre);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public string ValidarEmail(string email)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpValidarEmail);
            dbProvider.AddInParameter(command, helper.DesEmail, DbType.String, email);

            string id = null;
            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    int iIdUsuario = dr.GetOrdinal(helper.IdUsuario);
                    id = dr.GetString(iIdUsuario);
                }
            }
            return id;
        }

        public string EncriptarPassword(string password)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpEncriptarPassword);

            dbProvider.AddInParameter(command, helper.DesPassword, DbType.String, password);

            return (string)dbProvider.ExecuteScalar(command);
        }

        public string DesEncriptarPassword(string password)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpDesEncriptarPassword);

            dbProvider.AddInParameter(command, helper.DesPassword, DbType.String, password);

            return (string)dbProvider.ExecuteScalar(command);

        }
    }
}
